<?php

class DetectionModel extends Model
{

    public function punctuality()
    {
		 $data = array();
        
        $tag = 0; // 设置一个标记，可能有多个积分管理员没有设置积分权限
        
        $a = 1;
        
        // 查询部门
        if (M('Member_position')->where('site_id=' . __SITEID__)->find() == null) {
           
            $data['position']['status'] = "1";
            $data['position']['error'] = "本公司并未设置组织架构";
            $data['position']['url'] = U('Member_position/indexiframe');
        } else {
		
            $Member_posi = M('Member_position')->where('`site_id`=' . __SITEID__ . '  and  `name` like  "双击修改" ')->select();
            if ($Member_posi != null) {
                
                $data['position']['status'] = "1";
                $data['position']['error'] = "有" . count($Member_posi) . "个部门名字为双击修改";
                $data['position']['url'] = U('Member_position/indexiframe');
            }else{
				$data['position']['status'] = "0";
                $data['position']['error'] = "暂无错误";
                $data['position']['url'] = U('Member_position/indexiframe');
				}
        }
        
        // 查询成员
        if (M('Member')->where('site_id=' . __SITEID__)->find() == null) {
            $data['member']['status'] = "1";
            $data['member']['error'] = "本公司还没有员工";
            $data['member']['url'] = U('Employee/index');
        } else {
            
            // 积分管理员是否有权限
            $isAdmin = getManagerList();
            $Member = M('Member');
            $allMember = $Member->field('name,tel,memberpositionid')
                ->where('site_id=' . __SITEID__)
                ->select();
            if ($isAdmin != "") {
                foreach ($isAdmin as $key => $val) {
                    $isSetInte = $Member->field('id,name,integralconfig')
                        ->where('site_id=' . __SITEID__ . ' and id=' . $val)
                        ->find();
                    if ($isSetInte['integralconfig'] == null) {
                        if ($isSetInte['name'] != null) {
                            $data['admin' . $tag]['status'] = "1";
                            $data['admin' . $tag]['error'] = $isSetInte['name'] . "该用户并未设置积分权限";
                            $data['admin' . $tag]['url'] = U('Employee/index');
                            $tag ++;
                        }
                        if ($isSetInte['integralconfig'] != null && $tag == 0) {
                            $data['integralconfig']['status'] = "0";
                            $data['integralconfig']['error'] = "暂无错误";
                            $data['integralconfig']['url'] = "#";
                        }
                    }
                }
            } else {
                
                $data['admin']['status'] = "1";
                $data['admin']['error'] = "积分管理员并未设置";
                $data['admin']['url'] = U('Employee/add');
            }
            
            $e = 0;
            if ($allMember != null) {
                foreach ($allMember as $key => $val) {
                    if ($val['name'] == "" ||$val['tel'] == "" || $val['memberpositionid'] == "") {
                        $e ++;
                    }
                    
                }
				if($e!=0){
				$data['member']['status'] = "1";
				$data['member']['error'] = "有" . $e . "个员工没有填写完整信息";
				$data['member']['url'] = U('Employee/index');
            }
			}
        }
        
        // 是否操作表格
        $isSetTbale = M('Memberrequest')->where('site_id=' . __SITEID__)->find();
		
        if ($isSetTbale != "") {
	
            $data['table']['status'] = "0";
            $data['table']['error'] = "暂无错误";
            $data['table']['url'] = "#";

        } else {
			
            $data['table']['status'] = "1";
            $data['table']['error'] = "站点并未设定初始表格";
            $data['table']['url'] = U('Taskform/add');
        }
        
	
        $data['time'] = time();
        S('data', json_encode($data), 3600);
		
		}

    public function overTime($Action, $detearray)
    {
		   $data = json_decode(S('data'), true);
        
        $tag = 0; // 设置一个标记，可能有多个积分管理员没有设置积分权限积分

        switch ($Action) {
            case Position:
                {	
                    if ($detearray[Position] != 0) {
                        $detearray[Position] = 1;
                        return $detearray;
                        break;
                    }
                    
                    // 查询部门
                    if (M('Member_position')->where('site_id=' . __SITEID__)->find() == null) {
                        
                        $data['position']['status'] = "1";
                        $data['position']['error'] = "本公司并未设置组织架构";
                        $data['position']['url'] = U('Member_position/indexiframe');
                    } else {
                        $this->empData($data['position']);
                        $Member_posi = M('Member_position')->where('`site_id`=' . __SITEID__ . '  and  `name` like  "双击修改" ')->select();
                        if ($Member_posi != null) {
                            
                            $data['position']['status'] = "1";
                            $data['position']['error'] = "有" . count($Member_posi) . "个部门名字为双击修改";
                            $data['position']['url'] = U('Member_position/indexiframe');
                        } else {
                            
                            $data['position']['status'] = "0";
                            $data['position']['error'] = "暂无错误";
                            $data['position']['url'] = "#";
                        }
                    }
                    $detearray[Position] = 1;
                    S('data', null);
                    $data['time'] = time();
                    S('data', json_encode($data));
                    return $detearray;
                    continue;
                }
            case Admin:
                {

                    if ($detearray[Admin] != 0) {
                        
                        $detearray[Admin] = 1;
                        return $detearray;
                        break;
                    }
                    $this->empData($data['admin']); 			
                    // 积分管理员是否有权限
                    $isAdmin = getManagerList();
                    $Member = M('Member');
                    $allMember = $Member->field('name,position_id,memberpositionid')
                        ->where('site_id=' . __SITEID__)
                        ->select();
                    if ($isAdmin != "") {
                        			
                        foreach ($isAdmin as $key => $val) {
                            $isSetInte = $Member->field('id,name,integralconfig')
                                ->where('site_id=' . __SITEID__ . ' and id=' . $val)
                                ->find();
                            
                        
                            if ($isSetInte['name'] != null) {
                             
                                if ($isSetInte['integralconfig'] == null) {

                                    $data['admin' . $tag]['status'] = "1";
                                    $data['admin' . $tag]['error'] = $isSetInte['name'] . "该用户并未设置积分权限";
                                    $data['admin' . $tag]['url'] = U('Employee/edit', array(
                                        'id' => $isSetInte['id']
                                    ));
                                    $tag ++;
                        
								}
                                
                                if ($isSetInte['integralconfig'] != null && $tag == 0) {
                                    
                                    $tmp['member'] = $data['member'];
                                    $tmp['table'] = $data['table'];
                                    unset($data);
									
                                    $data['admin']['status'] = "0";
                                    $data['admin']['error'] = "暂无错误";
                                    $data['admin']['url'] = "#";
                                    $data['member'] = $tmp['member'];
                                    $data['table'] = $tmp['table'];
                                }
                            }
                        }
                    } else {
                        
                        $data['admin']['status'] = "1";
                        $data['admin']['error'] = "积分管理员并未设置";
                        $data['admin']['url'] = U('Employee/add');
                    }
                  
                    $detearray[Admin] = 1;
                    S('data', null);
                    $data['time'] = time();
                    S('data', json_encode($data));
                    return $detearray;
                    continue;
                }
            case Member:
                {
					
                    $Member = M('Member');
                    $allMember = $Member->field('name,group_id,memberpositionid')
                        ->where('site_id=' . __SITEID__)
                        ->select();
                    if ($detearray[Member] != 0) {
                        $detearray[Member] = 1;
                        return $detearray;
                        break;
                    }
                    $this->empData($data['member']);
                    // 查询成员
                    if (M('Member')->where('site_id=' . __SITEID__)->find() == null) {
                        $data['member']['status'] = "1";
                        $data['member']['error'] = "本公司还没有员工";
                        $data['member']['url'] = U('Employee/index');
                    } else {
                        $e = 0;
                        if ($allMember != null) {
                            foreach ($allMember as $key => $val) {
                                
                                if ($val['name'] == "" || $val['group_id'] == "" || $val['memberpositionid'] == "") {
                                    $e ++;
                                }
                                $data['member']['status'] = "1";
                                $data['member']['error'] = "有" . $e . "个员工没有填写完整信息";
                                $data['member']['url'] = U('Employee/index');
                            }
                        }
                    }
                    
                    $detearray[Member] = 1;
                    S('data', null);
                    $data['time'] = time();
                    S('data', json_encode($data));
                    return $detearray;
                    continue;
                }
            case Table:
                {
                    if ($detearray[Action] != 0) {
                        $detearray[Action] = "1";
                        return $detearray;
                        break;
                    }
                    $this->empData($data['table']);
                    $isSetTbale = M('Memberrequest')->where('site_id=' . __SITEID__)->find();
                    if ($isSetTbale != "") {
                        $data['table']['status'] = "0";
                        $data['table']['error'] = "暂无错误";
                        $data['table']['url'] = "#";
                    } else
                        $data['table']['status'] = "1";
                    $data['table']['error'] = "站点并未设定初始表格";
                    $data['table']['url'] = U('Taskform/add');
                }
                
                $detearray[Action] = 1;
                S('data', null);
                $data['time'] = time();
                S('data', json_encode($data));
                return $detearray;
                continue;
        }
    }

    public function empData($data)
    {
        if (isset($data)) {
            unset($data);
        }
    }
    
    // 超时时间，对比
    public function excTime($frontTime, $lastTime)
    {
        return floor($frontTime / 60) - floor($lastTime / 60);
    }
}